package com.ruoyi.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.domain.dto.SysRateDto;
import com.ruoyi.utils.DtoToEntityConverter;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.SysRateMapper;
import com.ruoyi.domain.SysRate;
import com.ruoyi.service.ISysRateService;

/**
 * 评分Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-05
 */
@Service
public class SysRateServiceImpl implements ISysRateService {
    @Autowired
    private SysRateMapper sysRateMapper;


    @Override
    public AjaxResult setRate(SysRateDto sysRateDto) {
        SysRate sysRate = DtoToEntityConverter.convertDtoToEntity(sysRateDto, SysRate.class);
        if (sysRateMapper.selectOne(sysRate.getUserId()) != null)
            return AjaxResult.error(201, "您已经评过分了，不能在评分了呦！");
        if (sysRateMapper.selectLend(sysRate.getBookId(), sysRate.getUserId()) == null)
            return AjaxResult.error(201, "不好意思，只有借过这本书的才能评分哦");
        int count = sysRateMapper.insertCount(sysRate.getId(), sysRate.getScore(), sysRate.getBookId(), sysRate.getUserId());
        return (count != 0) ? AjaxResult.success("评分成功") : AjaxResult.error(201, "评分失败");
    }

    @Override
    public AjaxResult getAvg(String bookId) {
//        JSONObject jsonObject = JSONObject.parseObject(bookId);
//        String string = jsonObject.getString(bookId);
//        int s = Integer.parseInt(String.valueOf(string));
//        Book book = JSONObject.parseObject(bookId, Book.class);
//        Long bookId1 = book.getBookId();
        ArrayList<Object> list = new ArrayList<>();
        List<SysRate> rateList = sysRateMapper.getRateList(Math.toIntExact(Long.parseLong(bookId)));
        double total = 0;
        int size = 0;
        double avg = 0;
        for (SysRate sysRate : rateList
        ) {
            total += sysRate.getScore();
            size++;
        }
        if (total == 0)
            avg = 0;
        else
            avg = total / size;
        return (avg != 0) ? AjaxResult.success("平均分是", avg) : AjaxResult.error("暂无均分");
    }

    @Override
    public AjaxResult getPeopleForScore(String bookId) {
//        HashMap<Integer, Integer> map = new HashMap<>();
//        Book book = JSONObject.parseObject(bookId, Book.class);
//        Long bookId1 = book.getBookId();
        ArrayList<Object> list = new ArrayList<>();
        for (int i = 5; i > 0; i--) {
            List<SysRate> scoreRank = sysRateMapper.getScoreRank(i, Long.valueOf(bookId));
            int size = scoreRank.size();
            list.add(size);
        }
        return (list != null) ? AjaxResult.success(list) : AjaxResult.error();
    }

    /**
     * 查询评分
     *
     * @param id 评分主键
     * @return 评分
     */
    @Override
    public SysRate selectSysRateById(Long id) {
        return sysRateMapper.selectSysRateById(id);
    }

    /**
     * 查询评分列表
     *
     * @param sysRate 评分
     * @return 评分
     */
    @Override
    public List<SysRate> selectSysRateList(SysRate sysRate) {
        return sysRateMapper.selectSysRateList(sysRate);
    }

    /**
     * 新增评分
     *
     * @param sysRate 评分
     * @return 结果
     */
    @Override
    public int insertSysRate(SysRate sysRate) {
        return sysRateMapper.insertSysRate(sysRate);
    }

    /**
     * 修改评分
     *
     * @param sysRate 评分
     * @return 结果
     */
    @Override
    public int updateSysRate(SysRate sysRate) {
        return sysRateMapper.updateSysRate(sysRate);
    }

    /**
     * 批量删除评分
     *
     * @param ids 需要删除的评分主键
     * @return 结果
     */
    @Override
    public int deleteSysRateByIds(Long[] ids) {
        return sysRateMapper.deleteSysRateByIds(ids);
    }

    /**
     * 删除评分信息
     *
     * @param id 评分主键
     * @return 结果
     */
    @Override
    public int deleteSysRateById(Long id) {
        return sysRateMapper.deleteSysRateById(id);
    }
}
